'''
Created on 20-mei-2013

@author: Michael
'''

from os import listdir
import cv2
import cv2.cv as cv
import os
import numpy as np
import fnmatch


#create display window
#cv2.namedWindow('Display Window')  

def getPanoramicImage(i):
    filename = ""
    if(i < 10):
        filename += "0"
    filename += str(i)
    img = cv2.imread("Panoramic images/"+filename+".tif")
    show = False
    if show:
        cv2.imshow('img', cv2.resize(img, (img.shape[1]/4,img.shape[0]/4)))
        cv2.waitKey(0)
    return img

def getPanoramicImages():
    images = []
    for filename in fnmatch.filter(os.listdir("Panoramic images"),'*.tif'):
        img = cv2.imread("Panoramic images/"+filename)
        images.append(img)
        show = False
        if show:
            cv2.imshow('img', cv2.resize(img, (img.shape[1]/4,img.shape[0]/4)))
            cv2.waitKey(0)
    return images
 
def getSegmentedImages():
    images = []
    for filename in fnmatch.filter(os.listdir("Panoramic images/segmented"),'*.png'):
        img = cv2.imread("Panoramic images/segmented/"+filename)
        images.append(img)
        show = False
        if show:
            cv2.imshow('img', cv2.resize(img, (img.shape[1]/4,img.shape[0]/4)))
            cv2.waitKey(0)
    return images

def writeImage(img, name):
    cv2.imwrite("Intermediary images/"+name, img)
    
def writeReportImage(img, name):
    cv2.imwrite("Report images/"+name, img)
    
def readCoords(coordinatesFile,teethImageNr):
    f = open(coordinatesFile, 'r')
    for i in range(0,teethImageNr):
        f.readline()
    coordinateString = f.readline()
    splitted = coordinateString.partition(',')
    row = int(splitted[0])
    column = int(splitted[2])
    return (row, column)